﻿using cn.school.api;
using cn.school.Models.Entities;
using cn.school.Models.Req;
using cn.school.Models.Res;
using Microsoft.AspNetCore.Mvc;

namespace cn.school.Restful.Controllers;

[ApiController]
[Route("api/[controller]")]
[Produces("application/json")]
public class StudentController: ControllerBase
{
    private readonly ILogger<StudentController> _logger;
    private readonly IStudentManage _studentManager;

    public StudentController(ILogger<StudentController> logger, IStudentManage studentManager)
    {
        _logger = logger;
        _studentManager = studentManager;
    }

    /// <summary>
    /// 添加学生
    /// </summary>
    /// <param name="student">输入的学生信息</param>
    /// <returns>插入后带有ID的学生信息实体类</returns>
    /// <response code="200">添加成功</response>
    [HttpPost]
    [ProducesResponseType(StatusCodes.Status200OK)]
    public CommonResponse AddOne(Student input)
    {
        if (input is null)
            throw new ArgumentNullException(nameof(input));
        if (input.Email is null)
            throw new ArgumentNullException("email", "不能为空");
        var data = _studentManager.AddOne(input);
        return CommonResponse.Success(data);
    }

    /// <summary>
    /// 根据ID查询单个学生的信息
    /// </summary>
    /// <param name="id">学生id</param>
    /// <returns>Student实体类</returns>
    /// <response code="200">查询成功</response>
    /// <response code="404">学生ID不存在，无法查询</response>
    [HttpGet("{id}")]
    [ProducesResponseType(StatusCodes.Status200OK)]
    [ProducesResponseType(StatusCodes.Status404NotFound)]
    public CommonResponse GetById(int id)
    {
        _logger.LogInformation("find {id} Student Info", id);
        var data = _studentManager.GetById(id);
        return CommonResponse.Success(data);
    }

    /// <summary>
    /// 按照条件查询学生列表
    /// </summary>
    /// <param name="email">学生注册的邮箱</param>
    /// <param name="name">学生名字</param>
    /// <param name="page">第几页</param>
    /// <param name="size">每页多少条</param>
    /// <returns></returns>
    [HttpGet("list")]
    [ProducesResponseType(StatusCodes.Status200OK)]
    public CommonResponse List(string? email, string? name, int page, int size)
    {
        StudentListReq req = new() { Email = email, Name = name, Page = page, Size = size };
        return CommonResponse.Success(_studentManager.List(req));
    }
}
